{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de824068",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "## 知识概念\n",
    "> 1.一切都是I/O：软工到产品的一般化知识  \n",
    "> 2.语音识别:speech recognition  \n",
    "> 3.语音唤醒  \n",
    "> 4.自动语音识别:Automatic Speech Recognition, 简称 ASR  \n",
    ">> 1.其目标是以电脑自动将人类的语音内容转换为相应的文字。与说话人识别及说话人确认不同，后者尝试识别或确认发出语音的说话人而非其中所包含的词汇内容。  \n",
    "\n",
    "> 1.语音合成:text to speech,简称 TTS  \n",
    ">> 1.将文字转化为语音的一种技术，类似于人类的嘴巴，通过不同的音色说出想表达的内容。 在语音合成技术中，主要分为 语言分析部分 和 声学系统部分 ，也称为 前端部分 和 后端部分， 语言分析部分主要是根据输入的文字信息进行分析，生成对应的语言学规格书，想好该怎么读；声学系统部分主要是根据语音分析部分提供的语音学规格书，生成对应的音频，实现发声的功能。  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c512572c",
   "metadata": {},
   "source": [
    "# 语音识别测试-百度API-ASR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8326f0e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6173477b",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# coding=utf-8\n",
    "\n",
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "\n",
    "    timer = time.perf_counter\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "    if sys.platform == \"win32\":\n",
    "        timer = time.clock\n",
    "    else:\n",
    "        # On most other platforms the best timer is time.time()\n",
    "        timer = time.time\n",
    "\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = 'audio/16k.wav'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "\n",
    "# 极速版\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "\n",
    "\n",
    "def fetch_token(API_KEY,SECRET_KEY):\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "#     print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "#     print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "#         print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "def asr(token,AUDIO_FILE):\n",
    "    speech_data = []\n",
    "    with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "        speech_data = speech_file.read()\n",
    "    length = len(speech_data)\n",
    "    if length == 0:\n",
    "        raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "    params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID}\n",
    "    #测试自训练平台需要打开以下信息\n",
    "    #params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "    params_query = urlencode(params);\n",
    "\n",
    "    headers = {\n",
    "        'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "        'Content-Length': length\n",
    "    }\n",
    "\n",
    "    url = ASR_URL + \"?\" + params_query\n",
    "#     print(\"url is\", url);\n",
    "#     print(\"header is\", headers)\n",
    "    # print post_data\n",
    "    req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "    try:\n",
    "        begin = timer()\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "        print(\"Request time cost %f\" % (timer() - begin))\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "\n",
    "    if (IS_PY3):\n",
    "        result_str = str(result_str, 'utf-8')\n",
    "#     print(result_str)\n",
    "    with open(\"result.txt\", \"w\") as of:\n",
    "        of.write(result_str)\n",
    "    return result_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0adc5c5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 语音识别执行如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4b13a6aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.c7638081d0d4059031a62accd550ff7b.2592000.1690124842.282335-19331335'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1cc858b",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr(xu_token,'16k.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf4cf1c7",
   "metadata": {},
   "source": [
    "# 语音识别自动回复文本机器人\n",
    "> 1.准备录制音频文件 （完成）  \n",
    "> 2.调用语音识别，将音频转成文本 （已完成）  \n",
    "> 3.文本自动回复 （已完成）原理: 问和答，I/O 输入和输出----特点： key:value  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bcbdddf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: SpeechRecognition in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (3.10.0)\r\n",
      "Requirement already satisfied: requests>=2.26.0 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from SpeechRecognition) (2.27.1)\r\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests>=2.26.0->SpeechRecognition) (1.26.9)\r\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests>=2.26.0->SpeechRecognition) (2021.10.8)\r\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests>=2.26.0->SpeechRecognition) (2.0.4)\r\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests>=2.26.0->SpeechRecognition) (3.3)\r\n"
     ]
    }
   ],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "83b77593",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: PyAudio in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (0.2.13)\n",
      "Collecting pipwin\n",
      "  Downloading pipwin-0.5.2.tar.gz (7.9 kB)\n",
      "  Preparing metadata (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25hCollecting docopt (from pipwin)\n",
      "  Downloading docopt-0.6.2.tar.gz (25 kB)\n",
      "  Preparing metadata (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25hRequirement already satisfied: requests in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from pipwin) (2.27.1)\n",
      "Collecting pyprind (from pipwin)\n",
      "  Downloading PyPrind-2.11.3-py2.py3-none-any.whl (8.4 kB)\n",
      "Requirement already satisfied: six in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from pipwin) (1.16.0)\n",
      "Requirement already satisfied: beautifulsoup4>=4.9.0 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from pipwin) (4.11.1)\n",
      "Collecting js2py (from pipwin)\n",
      "  Downloading Js2Py-0.74-py3-none-any.whl (1.0 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m253.5 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: packaging in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from pipwin) (21.3)\n",
      "Collecting pySmartDL>=1.3.1 (from pipwin)\n",
      "  Downloading pySmartDL-1.3.4-py3-none-any.whl (20 kB)\n",
      "Requirement already satisfied: soupsieve>1.2 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from beautifulsoup4>=4.9.0->pipwin) (2.3.1)\n",
      "Collecting tzlocal>=1.2 (from js2py->pipwin)\n",
      "  Downloading tzlocal-5.0.1-py3-none-any.whl (20 kB)\n",
      "Collecting pyjsparser>=2.5.1 (from js2py->pipwin)\n",
      "  Downloading pyjsparser-2.7.1.tar.gz (24 kB)\n",
      "  Preparing metadata (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25hRequirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from packaging->pipwin) (3.0.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests->pipwin) (1.26.9)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests->pipwin) (2021.10.8)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests->pipwin) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/linyuqing/opt/anaconda3/lib/python3.9/site-packages (from requests->pipwin) (3.3)\n",
      "Building wheels for collected packages: pipwin, docopt, pyjsparser\n",
      "  Building wheel for pipwin (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for pipwin: filename=pipwin-0.5.2-py2.py3-none-any.whl size=8790 sha256=efe22e04ff34c098e2993185100f0b8c7f0ac85971d7e79bb7e1b98cb451b3b3\n",
      "  Stored in directory: /Users/linyuqing/Library/Caches/pip/wheels/bc/86/30/f70db104d3f51560d9a177d6ced4f7e09f3e474d6985c101ae\n",
      "  Building wheel for docopt (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for docopt: filename=docopt-0.6.2-py2.py3-none-any.whl size=13723 sha256=045e9d25d26087df9374336c377d95db3c9f9e1465b3fa10993c3ef7cd5dc4ab\n",
      "  Stored in directory: /Users/linyuqing/Library/Caches/pip/wheels/70/4a/46/1309fc853b8d395e60bafaf1b6df7845bdd82c95fd59dd8d2b\n",
      "  Building wheel for pyjsparser (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for pyjsparser: filename=pyjsparser-2.7.1-py3-none-any.whl size=26000 sha256=df06e1b159af333826a2d15bfa554726389edf988eb4b8897a41106d8b6cccde\n",
      "  Stored in directory: /Users/linyuqing/Library/Caches/pip/wheels/f0/70/61/f42dc45dcf0fbe8c495ce579b04730787081499bfb5b8bc60e\n",
      "Successfully built pipwin docopt pyjsparser\n",
      "Installing collected packages: pySmartDL, pyprind, pyjsparser, docopt, tzlocal, js2py, pipwin\n",
      "Successfully installed docopt-0.6.2 js2py-0.74 pipwin-0.5.2 pySmartDL-1.3.4 pyjsparser-2.7.1 pyprind-2.11.3 tzlocal-5.0.1\n",
      "Usage:\n",
      "  pipwin install (<package> | [-r=<file> | --file=<file>]) [--proxy=<proxy>]\n",
      "  pipwin uninstall <package>\n",
      "  pipwin download (<package> | [-r=<file> | --file=<file>]) [-d=<dest> | --dest=<dest>] [--proxy=<proxy>]\n",
      "  pipwin search <package> [--proxy=<proxy>]\n",
      "  pipwin list\n",
      "  pipwin refresh [--log=<log>] [--proxy=<proxy>]\n",
      "  pipwin (-h | --help)\n",
      "  pipwin (-v | --version)\n"
     ]
    }
   ],
   "source": [
    "!pip install PyAudio\n",
    "\n",
    "!pip install pipwin\n",
    "!pipwin instll PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55c39538",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec622138",
   "metadata": {},
   "source": [
    "# 文本自动回复(自定义)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b5172825",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你好\":\"你好呀，有什么事么？\",\n",
    "    \"你叫什么名字\":\"我是人见人爱，花见花开的小小度呀\",\n",
    "    \"你多大了\":\"这是很私密的问题，我今年18岁了。\",\n",
    "    \"苹果用英文怎么说\":\"apple\",\n",
    "    \"今天天气\":\"我这边墨迹天气显示，今天广州30度，晴\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ea1456ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好呀，有什么事么？'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa.get('你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9a08f4f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我是人见人爱，花见花开的小小度呀'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa.get('你叫什么名字')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c686a20b",
   "metadata": {},
   "source": [
    "# 实践\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96b5f693",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 录制音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "# 通过调用电脑的麦克风进行音频的获取\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2. 调用百度asr\n",
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_result = eval(asr(xu_token,\"1.wav\"))['result'][0][:-1]  # eval()---> str to dict\n",
    "# asr_result\n",
    "\n",
    "# 3. 文本自动回复\n",
    "qa.get(asr_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
